Version 1.0 (07 August 2017)

Prerequisities

Prerequisities 1

What to know?

  1. Basic R skills
  2. Basic Unix/Linux skills

What to bring?

  1. Bring laptop
  2. A GitHub account?

Prerequisities 2

Which R packages to install/load?

pkg <- c("ggplot2", "rmarkdown", "knitr", "dplyr");
lapply(pkg, function(x) { if (!require(x, character.only = TRUE, quietly = TRUE)) {
  install.packages(x);
  require(x, quietly = TRUE)}
})

What data to work with?

We will use the starwars dataset from dplyr.

head(starwars, n = 4);
## # A tibble: 4 x 13
##             name height  mass hair_color  skin_color eye_color birth_year
##            <chr>  <int> <dbl>      <chr>       <chr>     <chr>      <dbl>
## 1 Luke Skywalker    172    77      blond        fair      blue       19.0
## 2          C-3PO    167    75       <NA>        gold    yellow      112.0
## 3          R2-D2     96    32       <NA> white, blue       red       33.0
## 4    Darth Vader    202   136       none       white    yellow       41.9
## # ... with 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
## #   films <list>, vehicles <list>, starships <list>

How to get started 1

Create new R Markdown document in RStudio

  1. File \(\rightarrow\) New File \(\rightarrow\) R Markdown

How to get started 2

  1. Specifiy output option

    Exercise: Create a new R Markdown document in RStudio

(R) Markdown 101

(R) Markdown 101

A note on different Markdown flavours: GitHub, R Markdown, vanilla Markdown, …

Focus here on R Markdown.

  1. Headers
  2. Emphasis: bold and italics
  3. Lists: Ordered and unordered
  4. Images and links
  5. Blockquotes

(R) Markdown 101: Headers

(R) Markdown 101: Images

  1. Use ![](path/to/image.png)
  2. Use HTML <img src="path/to/image.png" style="width:200px;">
  3. Use knitr::include_graphics

From Markdown to R Markdown

R Markdown output

  1. Word
  2. PDF
  3. HTML

Exercise: Create different output documents

The YAML header

  1. Title and subtitle
  2. Author
  3. Date
  4. Output option

More details for every output option

R Markdown elements

Controlling the R output

  1. Chunk options
  2. Plot dimensions
  3. Transparency

Exercise: Show different chunk options

Tables

  1. General: How to output tables/dataframes?
  2. knitr::kable()
  3. DT::datatable

Excercise: Output a table

Tables: knitr::kable()

suppressMessages(library(knitr));
kable(starwars[1:4, 1:4])
name height mass hair_color
Luke Skywalker 172 77 blond
C-3PO 167 75 NA
R2-D2 96 32 NA
Darth Vader 202 136 none

Tables: DT::datatable()

suppressMessages(library(DT));
#DT::datatable(starwars, options = list(dom = "ftp", scrollX = TRUE, pageLength = 4));
DT::datatable(starwars[, 1:4], options = list(pageLength = 8));

Interactive plots

  1. plotly
  2. D3 in R

Load the necessary libraries:

suppressMessages(library(ggplot2));
suppressMessages(library(plotly));
## Warning: package 'plotly' was built under R version 3.4.1
suppressMessages(library(scatterD3));

Interactive plots: plotly::ggplotly()

ggplotly(ggplot(starwars, aes(x = height, y = mass, label = name)) + geom_point(), height = 5);

Interactive plots: scatterD3::scatterD3()

scatterD3(x = starwars$height, y = starwars$mass, lab = starwars$name);

Beyond interactive plots

R Markdown and shiny

Link with github